/**
 * 
 */
package cx3k.GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

import cx3k.scheduler.Scheduler;
import cx3k.scheduler.objects.AcademicYear;
import cx3k.scheduler.objects.Schedule;

/**
 * @author Chris
 *
 */
public class SchedulePane extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Scheduler scheduler;
	public int scheduleID;
	private ClassInfo classInfo;
	private CompletedClasses completedClasses;
	private JTabbedPane tabs;
	private GridBagLayout gbl;
	private GridBagConstraints gbc;
	private JLabel totalCredits;
	private JLabel tcValue;
	private Box hbox;
	private SwapPane swap;
	private JLabel name;
	
	public SchedulePane(Scheduler s, ClassInfo classInfo, CompletedClasses cc) {
		this.setBackground(MainWindow.MAINCOLOR);
		this.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		gbl = new GridBagLayout();
		gbc = new GridBagConstraints();
		this.setLayout(gbl);
		this.scheduler = s;
		this.classInfo = classInfo;
		this.completedClasses = cc;
		this.tabs = new JTabbedPane();
		this.totalCredits = new JLabel("Scheduled Credits: ");
		this.tcValue = new JLabel("0");
		this.swap = new SwapPane(classInfo, s);
		swap.getJbSwap().addActionListener(new SwapListener());
		
		
		this.name = new JLabel("");
		gbc.gridx = 0;
		gbc.gridy = 0; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 1; 
		gbc.weighty = 1; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.CENTER; 
		gbl.setConstraints(this.name, gbc);
		this.add(this.name);
		
		gbc.gridx = 0;
		gbc.gridy = 1; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 100; 
		gbc.weighty = 90; 
		gbc.fill = GridBagConstraints.BOTH; 
		gbc.anchor = GridBagConstraints.CENTER; 
		gbl.setConstraints(this.tabs, gbc);
		this.add(this.tabs);
		
		hbox = Box.createHorizontalBox();
		hbox.add(totalCredits);
		hbox.add(tcValue);
		hbox.add(Box.createHorizontalStrut(100));
		hbox.add(swap);
		
		
		gbc.gridx = 0;
		gbc.gridy = 2; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 100; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.BOTH; 
		gbc.anchor = GridBagConstraints.LAST_LINE_START; 
		gbl.setConstraints(hbox, gbc);
		this.add(hbox);
		
	}
	
	public void clearSchedule() {
		this.remove(this.tabs);
		this.tabs = new JTabbedPane();
		gbc.gridx = 0;
		gbc.gridy = 1; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 100; 
		gbc.weighty = 90; 
		gbc.fill = GridBagConstraints.BOTH; 
		gbc.anchor = GridBagConstraints.CENTER; 
		gbl.setConstraints(this.tabs, gbc);
		this.add(this.tabs);
		this.updateUI();
	}
	
	public void loadSchedule(Schedule schedule) {
		//Schedule.printSchedule(schedule);
		
		this.remove(this.tabs);
		this.tabs = new JTabbedPane();
		this.scheduleID = schedule.getScheduleId();
		this.swap.setScheduleID(this.scheduleID);
		
		
		
		gbc.gridx = 0;
		gbc.gridy = 1; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 100; 
		gbc.weighty = 90; 
		gbc.fill = GridBagConstraints.BOTH; 
		gbc.anchor = GridBagConstraints.CENTER; 
		gbl.setConstraints(this.tabs, gbc);
		this.add(this.tabs);
		this.updateUI();
		
		this.tcValue.setText("0");
		AcademicYear years[] = schedule.getYears();
		for (int i = 0; i<years.length; i++) {
			this.tabs.addTab(years[i].getYearNumber(), new YearPane(years[i], classInfo, completedClasses, this.swap, this.tcValue));
		}
	}
	
	private class SwapListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			if (swap.getNumCourses() == 2) {
				System.out.println("Swap: " + scheduler.swapclass(scheduleID, swap.getSwap1().getCourseGroup(), swap.getSwap2().getCourseGroup()));
				swap.getHbox().remove(swap.getSwap1());
				swap.getHbox().remove(swap.getSwap2());
				swap.setSwap1(new SwapClassDisplay());
				swap.setSwap2(new SwapClassDisplay());
				swap.setNumCourses(swap.getNumCourses() - 2);
				swap.getHbox().add(swap.getSwap1(), 1);
				swap.getHbox().add(swap.getSwap2(), 2);
				swap.updateUI();
				
				loadSchedule(scheduler.getSchedule(scheduleID));
			}
		}
		
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

